//
//  ImportKeyViewController.m
//  iHealthEHR
//
//  Created by Joseph Ayo Akinyele on 7/1/10.
//  Copyright 2010 Johns Hopkins University. All rights reserved.
//

#import "ImportKeyViewController.h"


@implementation ImportKeyViewController
@synthesize username, password, authtoken;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(IBAction) removeKeyboard:(id) sender
{
	[sender resignFirstResponder];
}

-(IBAction) backgroundClick:(id) sender
{
	[username resignFirstResponder];
	[password resignFirstResponder];
	[authtoken resignFirstResponder];
}

-(void) readKey:(NSString *) user
{
	// find key with 'user'-abe on file system?
}

-(IBAction) importButtonPressed:(id) sender
{
	// start the activity indicator
	NSString *_username = username.text;
	NSString *_password = password.text;
	NSString *_authtoken = authtoken.text;
	NSArray *objects = [[NSArray alloc] initWithObjects:_username, _password, _authtoken, nil];
	
	NSLog(@"Username: %@, Passphrase: %@, Token: %@", _username, _password, _authtoken);

	// concatenate the inputs to generate the key via sha1 hash?
	HashInterface *hash_func = [[HashInterface alloc] init];
	
	unsigned char *hash_value = [hash_func compute_sha2_hash:objects];
	[objects release];
	[hash_func release];
	free(hash_value); // symm key
	
	// read in the file from the docs directory of app: validate by performing import key with CPABEInterface
	[self readKey:_username];
	
	// base-64 decode then decrypt the encrypted key
	
	
	// ask user to enter another passphrase separate from password
	
	
	// import the key into the keychain and let user know that is complete!
	
	// after processing
	username.text = @"";
	password.text = @"";
	authtoken.text = @"";
	// stop the activity indicator and display status that the 
}

@end
